package com.nuanshui.heatedloan.dubbo.extension;

import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.framework.springContext.SpringContextUtil;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;


public class DubboContextManager {

    private final static ConcurrentMap<String, ReferenceBean<?>> referenceConfigs = new ConcurrentHashMap<String, ReferenceBean<?>>();

    /***
     *
     * @param referenceClass   原始的接口类型
     * @return 返回的是接口的代理对象
     */
    public static <T> T getDubboBean(Class<T> referenceClass) {
        return getDubboBean(referenceClass,"");
    }

    /**
     *
     * @param referenceClass  DUBBO服务接口
     * @param version  DUBBO服务版本
     * @param <T>
     * @return
     */
    public static <T> T getDubboBean(Class<T> referenceClass,String version) {
        if (referenceClass == null) {
            throw new IllegalStateException("请输入接口类型");
        } else if (!referenceClass.isInterface()) {
            throw new IllegalStateException("The @Reference undefined interfaceClass or interfaceName, and the property type " + referenceClass.getName() + " is not a interface.");
        }
        String interfaceName = referenceClass.getName();
        String key = interfaceName;
        ReferenceBean<?> referenceConfig = referenceConfigs.get(key);
        if (referenceConfig == null) {
            referenceConfig = new ReferenceBean<Object>();
            referenceConfig.setInterfaceClass(referenceClass);
        }
        if (SpringContextUtil.getApplicationContext() != null) {
            referenceConfig.setApplicationContext(SpringContextUtil.getApplicationContext());
            try {
                referenceConfig.afterPropertiesSet();
            } catch (RuntimeException e) {
                throw (RuntimeException) e;
            } catch (Exception e) {
                throw new IllegalStateException(e.getMessage(), e);
            }
        }
        referenceConfigs.putIfAbsent(key, referenceConfig);
        referenceConfig = referenceConfigs.get(key);
        referenceConfig.setVersion(version);
        T obj=null;
        try {
            if(referenceConfig!=null) {
                obj=(T) referenceConfig.get();
            }
        } catch (Exception e) {

        }
        return obj;
    }

    public static <T> T getBean(final Class<T> clazz) {
        return getBean(clazz,false);
    }

    public static <T> T getBean(final Class<T> clazz,boolean bDubbo) {

        T obj = null;
        if (SpringContextUtil.getApplicationContext() != null) {
            obj = SpringContextUtil.getBean(clazz);
        }

        if (obj == null && bDubbo) {
            obj = getDubboBean(clazz);
        }
        return obj;
    }

    /**
     *
     * @param clazz  服务接口
     * @param bDubbo  是不是DUBBO服务
     * @param version  服务版本
     * @param <T>
     * @return
     */
    public static <T> T getBean(final Class<T> clazz,boolean bDubbo,String version) {

        T obj = null;
        if (SpringContextUtil.getApplicationContext() != null) {
            obj = SpringContextUtil.getBean(clazz);
        }
        if (obj == null && bDubbo && SysStringUtils.isNotEmpty(version)) {
            obj = getDubboBean(clazz,version);
        }
        return obj;
    }
}
